package com.lagou.web.servlet;

import com.lagou.base.Constants;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.impl.CourseServiceImpl;
import com.lagou.utils.DateUtils;
import com.lagou.utils.UUIDUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courseSaleInfo")
public class CourseSaleInfoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            Course course = new Course();

            Map<String, Object> map = new HashMap<>();

            DiskFileItemFactory factory = new DiskFileItemFactory();

            ServletFileUpload upload = new ServletFileUpload(factory);

            List<FileItem> list = upload.parseRequest(req);

            for (FileItem item : list) {

                boolean formField = item.isFormField();
                if (formField) {
                    String name = item.getFieldName();
                    String value = item.getString("UTF-8");
                    System.out.println(name + ":" + value);
                    map.put(name, value);
                } else {

                    String fileName = item.getName();
                    String newFileName = UUIDUtils.getUUID() + "_" + fileName;

                    InputStream in = item.getInputStream();

                    String realPath = this.getServletContext().getRealPath("/");
                    String home = realPath.substring(0, realPath.indexOf("lagou_edu_home"));
                    OutputStream out = new FileOutputStream(home + "/upload/" + newFileName);

                    IOUtils.copy(in, out);
                    out.close();
                    in.close();

                    map.put("course_img_url", Constants.URL_BASE +  "/upload/" + newFileName);
                }
            }

            BeanUtils.populate(course, map);

            String dateFormat = DateUtils.getDateFormat();
            course.setCreate_time(dateFormat);
            course.setUpdate_time(dateFormat);

            course.setStatus(1);

            CourseService cs = new CourseServiceImpl();
            String result = null;
            if (0 != course.getId()) {
                result = cs.updateCourseSaleInfo(course);
            } else {
                result = cs.saveCourseSalesInfo(course);
            }

            resp.getWriter().print(result);
        } catch (FileUploadException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
